package artid.com.arttogo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.RqCertif;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.task.CallbackSky;

public class ScanMiroViewAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	CallbackSky cs;
	public List<RqCertif.PointImg> listdata;
	public List<String> listX,listY,listInfo;
	Boolean isRound = false;
//	String iconUrl;
	public ScanMiroViewAdapter(Context mContext, Boolean isRound) {
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		listdata  = new ArrayList<RqCertif.PointImg>();
		listX = new ArrayList<String>();
		listY = new ArrayList<String>();
		listInfo = new ArrayList<String>();
		this.isRound = isRound;
		
	}

	public void setdata(List<RqCertif.PointImg> listdata){
		this.listdata = listdata;
//		iconUrl = iconurl;
		notifyDataSetChanged();
	}
	

	@Override
	public int getCount() {
		return listdata.size();  
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_microview, null);
			vh.imgIndex = (ImageView)convertView.findViewById(R.id.item_scan_imgindex);
			vh.mainImg = (ImageView)convertView.findViewById(R.id.item_scan_img_miro);
			vh.index = (TextView)convertView.findViewById(R.id.item_scan_index);
			vh.x = (TextView)convertView.findViewById(R.id.item_scan_x);
			vh.y = (TextView)convertView.findViewById(R.id.item_scan_y);
			vh.info = (TextView)convertView.findViewById(R.id.item_scan_info);
			vh.tag = (TextView)convertView.findViewById(R.id.item_scan_imgtag);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		
		final RqCertif.PointImg item = listdata.get(position);
		vh.index.setText(String.valueOf(position+1));
		vh.info.setText(item.getText());
		if (!isRound) {
			vh.imgIndex.setImageResource(R.drawable.smjg_ico_fang);
			vh.tag.setText("局部图");
		}else {
			vh.imgIndex.setImageResource(R.drawable.ico_quanquan);
			vh.tag.setText("微观图");
		}
//		String xy = item.getText();
//		try {
//			String x = xy.substring(xy.indexOf("X=")+2);
//			x = x.substring(0,x.indexOf(","));
//			vh.x.setText("X:"+x+"cm");
//			String y = xy.substring(xy.indexOf("Y=")+2);
//			xy = y;
//			y = y.substring(0,y.indexOf(";"));
//			vh.y.setText("Y:"+y+"cm");
//			vh.info.setText(xy.substring(xy.indexOf(";")+1));
//		} catch (Exception e) {
//			// TODO: handle exception
//			vh.x.setText("X:暂无数据");
//			vh.y.setText("Y:暂无数据");
//		}
		
		
		Glide.with(mContext).load(item.getImgurl()+ AppUtil.picWidthSmallUrl)
		.placeholder(R.drawable.ic_menu_rotate)
		.error(R.drawable.ic_menu_rotate).into(vh.mainImg);

		return convertView;
	}
	
	public String getImgurl(int i){
		return listdata.get(i).getImgurl();
	}

	public String getX(int i) {
		final RqCertif.PointImg item = listdata.get(i);
		
		String xy = item.getText();
		try {
			String x = xy.substring(xy.indexOf("X=")+2);
			x = x.substring(0,x.indexOf(","));
			return "X:"+x+"cm";
			
		} catch (Exception e) {
			// TODO: handle exception
			return "X:暂无数据";
			
		}
	}
	
	public String getY(int i) {
		final RqCertif.PointImg item = listdata.get(i);
	
		String xy = item.getText();
		try {
			
			String y = xy.substring(xy.indexOf("Y=")+2);
			xy = y;
			y = y.substring(0,y.indexOf(";"));
			return "Y:"+y+"cm";
		} catch (Exception e) {
			// TODO: handle exception
			return "Y:暂无数据";
		}
	}
	
	public String getInfo(int i) {
		final RqCertif.PointImg item = listdata.get(i);
		
		String xy = item.getText();
		try {
			
			String y = xy.substring(xy.indexOf("Y=")+2);
			xy = y;
			y = y.substring(0,y.indexOf(";"));
			
			return xy.substring(xy.indexOf(";")+1);
		} catch (Exception e) {
			// TODO: handle exception
			return item.getText();
		}
	}
	
	class ViewHolder {
		TextView index,x,y,info,tag;
		ImageView mainImg,imgIndex;

	}

	
	
	
}
